//
//  DataModel.m
//  PCSchool
//
//  Created by Arindam Ghosh on 9/5/13.
//  Copyright (c) 2013 Jayanta. All rights reserved.
//

#import "DataModel.h"

#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
#import "Reachability.h"
#import "PCSchoolAppDelegate.h"
#import "ConnectionManager.h"

@implementation DataModel

@synthesize appDelegate;

//@synthesize responseDidfinishHandeller;

@synthesize requestSent;
@synthesize conObj;

-(id)init{
	if((self = [super init])){
		
		self.requestSent = [NSMutableSet set];
		self.appDelegate=(PCSchoolAppDelegate *)[[UIApplication sharedApplication] delegate];
        
		
	}
	return self;
}


-(BOOL)sendRequestFor:(NSString*)aRequestPage from:(id)aSource
{
	//id tempObj = nil;
    
//    if ([self.appDelegate.hostReach currentReachabilityStatus] == NotReachable) {
//        [aSource notifyRequesterWithData:nil];
//        //[aSource showAlertMessage:@"The Internet connection appears to be offline."];
//        [self.appDelegate performSelector:@selector(removeLoadingView)];
//        
//        return NO;
//    }
    
    
    if([self.requestSent containsObject:aRequestPage]){
        NSLog(@"%@ %@",@"Request not sent",aRequestPage);
        return NO;
    }
	
	if ([aRequestPage isEqualToString:LOGIN] ){
		[self.requestSent addObject:LOGIN];
	}
    else if ([aRequestPage isEqualToString:SCHOOL_LIST] ){
		[self.requestSent addObject:SCHOOL_LIST];
	}
	
	ConnectionManager* aConn = [[ConnectionManager alloc] init];
    conObj = aConn;
	
	aConn.requestName = aRequestPage;
	aConn.requester = aSource;
	aConn.handler = self;
	
	NSLog(@"Request sent %@ For event ",aRequestPage);
	
	BOOL isSent = [aConn startRequest];
	if (!isSent) {
		[aConn release];
	}else{
        [requestSent addObject:aRequestPage];
        NSLog(@"%@ %@",@"Request sent",aRequestPage);
    }
	return isSent;
}




-(void)notifyRequestFinished:(id) aManager{
	ConnectionManager* aCon = aManager;
	
	NSString* aRequest = aCon.requestName;
    NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>> CONNECTIUON REQUEST NAME    %@",aRequest);
    
	if ([aRequest isEqualToString:LOGIN] ) {
		if ([self.requestSent containsObject:LOGIN]) {
			[self.requestSent removeObject:LOGIN];
            [aCon.requester notifyRequesterWithData:aCon];

			
		}else {
			return;
		}
        
		
	}
    
    else if ([aRequest isEqualToString:SCHOOL_LIST] ) {
		if ([self.requestSent containsObject:SCHOOL_LIST]) {
			[self.requestSent removeObject:SCHOOL_LIST];
            [aCon.requester notifyRequesterWithData:aCon];
            
			
		}else {
			return;
		}
        
		
	}
    else if ([aRequest isEqualToString:STUDENT_UDID_UPDATE] ) {
		if ([self.requestSent containsObject:STUDENT_UDID_UPDATE]) {
			[self.requestSent removeObject:STUDENT_UDID_UPDATE];
            [aCon.requester notifyRequesterWithData:aCon];
            
			
		}else {
			return;
		}
        
		
	}
    else if ([aRequest isEqualToString:CLOSEST_STUDENT_DETAILS] ) {
		if ([self.requestSent containsObject:CLOSEST_STUDENT_DETAILS]) {
			[self.requestSent removeObject:CLOSEST_STUDENT_DETAILS];
            [aCon.requester notifyRequesterWithData:aCon];
            
			
		}else {
			return;
		}
        
		
	}
    
}

-(void)notifyRequestFailed:(id) aManager
{
    
}




-(void)dealloc
{
    [conObj release];
    [requestSent release];
    [super dealloc];
}



@end
